<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use Redis;
use swoole_client;
class Swooletest extends controller
{
  	public function index(){
        $url = url('index/swooletest/test','','','119.23.33.37');
    	$params=[
            ['id'=>1,'url'=>$url],
            ['id'=>2,'url'=>$url],
            ['id'=>3,'url'=>$url],
            ['id'=>4,'url'=>$url],
            ['id'=>5,'url'=>$url],
            ['id'=>6,'url'=>$url],
            ['id'=>7,'url'=>$url],
            ['id'=>8,'url'=>$url],
            ['id'=>9,'url'=>$url],
            ['id'=>10,'url'=>$url],
            ['id'=>11,'url'=>$url],
            ['id'=>12,'url'=>$url],
        ];//接口数据
        $msg = json_encode($params);

        /*include '../extend/swoole/Client.php';
        $client = new \Client();
        $client->send($msg);
        echo "[".date("Y-m-d H:i:s")."]执行完成";*/
        $client = new swoole_client(SWOOLE_SOCK_TCP);
        //连接到服务器
        if (!$client->connect('127.0.0.1', 9501, 0.5))
        {
            $this->write("链接失败");
        }
        //向服务器发送数据
        if (!$client->send($msg))
        {
            $this->write("发送失败");
        }
        //关闭连接
        $client->close();
        echo  '完成';
    }
    public function add(){
        #操作数据库
        $id = input('id');
        echo $id;
        #$data = DB::query('select * from db_input where 1 ');
        
    }
    public function test(){
        set_time_limit(0);
        $i=input('id')?input('id'):1;
        file_put_contents('../data.txt',var_export($i,true)."\r\n",FILE_APPEND);
        while ( $i<= 20) {
            echo $i;
            echo '<br>';
            $data = ['title' => $i.'-'.date('Y-m-d H:i:s'), 'num' => $i];
            Db::name('input')->insert($data);
            sleep(1);
            $i++;

        }
        echo '完成';
    }
    public function write($error){
        #错误 
        $error .= date('Y-m-d H:i:s');
        $path = '../error.txt';
        file_put_contents($path,$error,FILE_APPEND);
    }

}